# Contributor: prspkt <prspkt@protonmail.com>
# Maintainer: prspkt <prspkt@protonmail.com>
pkgname=xscreensaver
pkgver=6.04
pkgrel=0
pkgdesc="Screensavers for X11 environment"
url="https://www.jwz.org/xscreensaver/"
arch="all"
license="GPL-2.0-or-later GPL-3.0-or-later 0BSD MIT"
depends="bc"
options="suid"
makedepends="gtk+2.0-dev libjpeg-turbo-dev mesa-dev gettext-dev libxmu-dev perl
	libxml2-dev libxi-dev libxinerama-dev libxrandr-dev glu-dev gdk-pixbuf-xlib-dev
	"
subpackages="$pkgname-doc $pkgname-gl-extras:gl $pkgname-extras"
source="https://www.jwz.org/xscreensaver/xscreensaver-$pkgver.tar.gz"

_libexecdir=/usr/libexec/xscreensaver
_confdir=/usr/share/xscreensaver/config

# secfixes:
#   6.02-r0:
#     - CVE-2021-34557

build() {
	# https://bugs.gentoo.org/830914
	[ "$CARCH" = "aarch64" ] && export CFLAGS="$CFLAGS -flax-vector-conversions"

	./configure \
		--build=$CBUILD \
		--host=$CHOST \
		--prefix=/usr \
		--sysconfdir=/etc \
		--mandir=/usr/share/man \
		--infodir=/usr/share/info \
		--with-gtk \
		--with-xinerama-ext \
		--with-xf86vmode-ext \
		--with-randr-ext
	make
}

package() {
	make install_prefix="$pkgdir" install
}

gl() {
	local _conf
	pkgdesc="An enhanced set of screensavers that require OpenGL"
	cd "$pkgdir"
	mkdir -p "$subpkgdir"/$_confdir
	scanelf -Rn . | awk '$2 ~ /libE?GL/ { print $3 }' | while read -r f; do
		mkdir -p "$subpkgdir"/${f%/*}
		mv "$f" "$subpkgdir"/${f%/*}

		_conf=usr/share/xscreensaver/config/${f##*/}.xml
		if [ -f "$_conf" ]; then
			mv "$_conf" "$subpkgdir"/$_confdir
		fi
	done
}

extras() {
	pkgdesc="An enhanced set of screensavers"
	mkdir -p "$subpkgdir/$_libexecdir" "$subpkgdir/$_confdir"

	local _path
	for _path in "$pkgdir/$_libexecdir"/*; do
		expr "$_path" : \
			'.*/xscreensaver-\(auth\|gfx\)$' > /dev/null || \
			mv "$_path" "$subpkgdir/$_libexecdir"
	done

	mv "$pkgdir/$_confdir"/* "$subpkgdir/$_confdir"
}

sha512sums="
7e8f01853b3d9252ce0120894db7dd4fa3cd19114602a09aa770ec750e2f3742585ca4daf19b009e97386f8c0e3cc89330fe760148c6388f41de8cc1f48f8071  xscreensaver-6.04.tar.gz
"
